package name.flurl.android.tt_news4android;

import java.net.URL;

import name.flurl.android.tt_news4android.Tt_news4AndroidApplication.CONSTANTS;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAdapter extends ArrayAdapter<NewsEntry> {
	private Activity mContext;
	private NewsEntry[] mEvents = null;
	private BaseNewsParser mParser;
	private int mOffset = 0;
	private URL mUrl = null;
	private Handler mActivityHandler;

	public NewsAdapter(NewsListActivity context, URL url,
			Handler loadingDlgHandler) {
		super(context, R.layout.news_list_item);
		mContext = context;
		mUrl = url;
		mActivityHandler = loadingDlgHandler;
		mParser = new JsonNewsParser(this, mUrl);
		showLoadingDlg();
		mParser.execute(0);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = mContext.getLayoutInflater();
		View row = inflater.inflate(R.layout.news_list_item, parent, false);

		TextView date = (TextView) row.findViewById(R.id.newsdate);
		date.setText(mEvents[position].getmDateTimeF());

		TextView categories = (TextView) row.findViewById(R.id.newscategories);
		if (!mEvents[position].getmCategories().equals("")) {
			categories.setText(mEvents[position].getmCategories());
			categories.setVisibility(View.VISIBLE);
		} else {
			categories.setVisibility(View.GONE);
		}

		TextView title = (TextView) row.findViewById(R.id.newstitle);
		title.setText(mEvents[position].getmTitle());

		TextView teaser = (TextView) row.findViewById(R.id.newsteaser);
		teaser.setText(mEvents[position].getTeaser());

		ImageView galleryIcon = (ImageView) row
				.findViewById(R.id.newsgalleryicon);
		if (mEvents[position].hasCategory("Gallery"))
			galleryIcon.setVisibility(View.VISIBLE);
		else
			galleryIcon.setVisibility(View.INVISIBLE);

		return (row);

	}

	public void addEvents(NewsEntry[] news) {
		dismissLoadingDlg();
		if (news != null) {

			int len = mEvents == null ? 0 : mEvents.length;

			NewsEntry[] tmpEvents = new NewsEntry[len + news.length];

			// this is for merging old and new newss

			for (int i = 0; i < len; i++) {
				tmpEvents[i] = mEvents[i];
			}

			for (int j = 0; j < news.length; j++) {
				add(news[j]);
				tmpEvents[len + j] = news[j];
			}

			mEvents = tmpEvents;
			// ((EventsListActivity) mContext).fillData();

		} else {
			sendMessageToActivity(CONSTANTS.MSG_EOF);
		}
	}

	public NewsEntry[] getEvents() {
		return mEvents;
	}

	public void loadMoreEvents() {
		showLoadingDlg();
		mOffset += 1;
		mParser = new JsonNewsParser(this, mUrl);
		mParser.execute(mOffset);
	}

	private void showLoadingDlg() {
		// if (mProgressDialog == null) {
		// mProgressDialog = new ProgressDialog(mContext);
		// mProgressDialog.setOwnerActivity(mContext);
		// mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// mProgressDialog.setMessage("Loading...");
		// mProgressDialog.setCancelable(false);
		// mProgressDialog.show();
		// }
		sendMessageToActivity(CONSTANTS.MSG_LOADING);
	}

	private void dismissLoadingDlg() {
		// if (mProgressDialog != null) {
		// mProgressDialog.dismiss();
		// mProgressDialog = null;
		// }
		sendMessageToActivity(CONSTANTS.MSG_LOADING_FINISHED);
	}

	private void sendMessageToActivity(int message) {
		Message msg = mActivityHandler.obtainMessage();
		Bundle b = new Bundle();
		b.putInt("action", message);
		msg.setData(b);
		mActivityHandler.sendMessage(msg);
	}

	public Context getmContext() {
		return mContext;
	}
}
